import { View, Text, Button } from "@tarojs/components";
import Taro, { useLoad } from "@tarojs/taro";
import "./index.scss";

export default function Index() {
  const intance = Taro.getCurrentInstance();

  useLoad(() => {
    console.log("Page loaded.");
  });

  function goBack() {
    Taro.navigateBack({
      delta: 1,
    });

    if (process.env.TARO_ENV === "weapp") {
      // const pages = Taro.getCurrentPages()
      // const page = pages[pages.length - 1]
      // 注意：这中方式只支持微信小程序端
      const eventChannel = intance.page?.getOpenerEventChannel?.();
      eventChannel?.emit("acceptDataFromDetail02Page", {
        data: "拿到detail02传递到02_router-param页面的数据",
      });
    }
  }

  return (
    <View className="index">
      <Text>Hello world! detail02</Text>
      <Button onClick={goBack}>返回</Button>
    </View>
  );
}
